﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.Taxonomy;
using SPContrib.Extensions;

namespace SPContrib.SharePoint.Taxonomy.Extensions
{
    public static class GroupExtensions
    {

        public static TermSet EnsureTermSet(this Group instance, string termSetName)
        {
            TermSet ts;
            bool match = instance
                            .FindTermSets(termSetName)
                            .Execute(out ts);

            if (match == false)
            {
                ts = instance.CreateTermSet(termSetName);
            }

                return ts;
        }

        public static IEnumerable<TermSet> FindTermSets(this Group instance, string termSetName)
        {
            return from termset in instance.TermSets
                   where termset.Name.Equals(termSetName, StringComparison.OrdinalIgnoreCase)
                   select termset;
        }

    }
}
